import { Router } from 'express';
import config from '../../../../config';
import { Code, Response, Message } from '../../../../interfaces/response';
import { RunScript } from '../../../RunScript';
const router: Router = Router();
router.post('/webhook', (request, response) => {
  const password = request.headers['x-gitee-token'];
  const { body } = request;
  if (password === config.webHookPwd && body?.action === 'merge') {
    if (body?.source_repo?.project?.name === "WhatsApp") {
      RunScript('sh', [config.scriptPath, `${config.sourcePath}/whats-app`,
      `${config.sourcePath}/whats-app/frontEnd,0000,0000`,
      `${config.sourcePath}/whats-app/backEnd,8081,${config.sourcePath}/whats-app/backEnd/build/app.js`], () => {
        console.log('run script success.');
      });
    }
    const res: Response = {
      code: Code.SUCCESS,
      msg: Message.SUCCESS
    }
    response.send(res);

  } else {
    const res: Response = {
      code: Code.BADREQUEST,
      msg: Message.BADREQUEST
    }
    response.send(res);
  }
});
export default router;